package org.idea.iedis.framework.server.dict;

/**
 * @Author linhao
 * @Date created in 11:48 上午 2021/4/30
 */
public class HashMap<K, V> implements IMap<K, V> {

    private Entry<K, V>[] table = null;
    private int defaultSize = 16;
    private int currentSize = defaultSize;
    private int useSize = 0;

    public HashMap() {
        this.table = new Entry[defaultSize];
    }

    public HashMap(int size) {
        this.table = new Entry[size];
        this.currentSize = size;
    }

    public int getUseSize() {
        return useSize;
    }

    public int getCurrentSize() {
        return currentSize;
    }

    public Entry<K, V>[] getTable() {
        return table;
    }

    public void setTable(Entry<K, V>[] table) {
        this.table = table;
    }

    @Override
    public V get(K key) {
        if (key == null) {
            return null;
        }
        int index = hash(key) & (currentSize - 1);
        Entry<K, V> entries = table[index];
        if (entries == null) {
            return null;
        }
        return entries.getVal();
    }

    @Override
    public V put(K key, V val) {
        int index = hash(key) & (currentSize - 1);
        System.out.println(currentSize);
        Entry<K, V> entries = table[index];
        if(entries==null){
            useSize++;
//            System.out.println(useSize);
            Entry<K, V> newItem = new Entry<>(key, val, null);
            table[index] = newItem;
            return val;
        }
        while (entries!=null && entries.getNext() != null) {
            entries = entries.getNext();
        }
        Entry<K, V> newItem = new Entry<>(key, val, null);
        entries.setNext(newItem);
        return val;
    }

    /**
     * 自定义hash计算
     *
     * 思路1 采用轮训存储到不同槽位，扩容槽位的时候需要迁移数据，比较麻烦
     * 假设使用取模的方式定位key到不同到槽位，在后期扩容的时候不能准确计算出所在位置
     * <p>
     * 高16位一般不会影响计算结果
     **/
    static final int hash(Object key) {
        int h;
        //大部分情况下数组的大小都是小于2^16所以这里其实在后续做定位的时候，没有必要将所有的hashcode都和数组体积做异或运算
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

    static final int indexOf(int hash, int length) {
        return hash & (length - 1);
    }

    public static void main(String[] args) {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("k1", "v1");
        hashMap.put("k2", "v2");
        System.out.println(hashMap.get("k1"));
        System.out.println(hashMap.get("k2"));
//        HashMap<String,String> map = new HashMap<>();
//        for(int i=0;i<19;i++) {
//            map.put(""+i,""+i);
//        }
        java.util.HashMap map;
        for(int i=0;i<1000;i++){
            int hashCode = hash(i);
            int result = indexOf(hashCode,24);
            System.out.println(result);
        }
//        System.out.println(Integer.toBinaryString(2));
//        System.out.println(Integer.toBinaryString(8));
//        System.out.println(2&8);
//        System.out.println(hash(21));
//        System.out.println(17%10);
//        System.out.println(0 >>> 16);
    }
}
